package icasue.reflect.handles.method;

import icasue.reflect.annotations.NotNull;

import java.lang.reflect.Method;

class Tools{
    /**
     * 在目标类中寻找某个方法.
     * @param type
     * @param mName
     * @param paramClassAry
     * @return
     */
    static Method discoveryMethod(Class type, @NotNull String mName, Class[] paramClassAry){
        Method method = null;
        try {
            method = type.getDeclaredMethod(mName,paramClassAry);
        }catch (Throwable e){
            method = null;
        }
        // 从继承 -> 接口进行寻找.
        if( method == null ){
            Class superclass = type.getSuperclass();
            if(superclass != null){
                method = discoveryMethod(superclass,mName,paramClassAry);
            }
            if(method == null){
                Class[] interfaces = type.getInterfaces();
                if(interfaces != null && interfaces.length > 0){
                    for (Class anInterface : interfaces) {
                        method = discoveryMethod(anInterface,mName,paramClassAry);
                        if(method != null){
                            break;
                        }
                    }
                }
            }
        }
        if(method != null){
            method.setAccessible(true);
        }
        return method;
    }
}